package com.study.zk.lock.simple;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * 多线程，模拟高并发，分布式场景
 *
 * @author: chenhs
 * @date: Created in 7:05 2020/2/4
 **/
public class ConcurrentTestDistribteDemo {


    public static void main(String[] args) {
        //并发数
        int currency = 50;

        //循环屏障
        CyclicBarrier cb = new CyclicBarrier(currency);

        OrderService orderService = new OrderServiceImplWithDisLock();

        //多线程，模拟高并发
        for (int i = 0; i < currency; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    //模拟分布式集群场景（一个线程相当于一个tomcat）
                    //OrderService orderService = new OrderServiceImplWithLock();
                    //OrderService orderService = new OrderServiceImplWithDisLock();

                    System.out.println(Thread.currentThread().getName() + "---------我准备好了！");
                    //等待一起出发
                    try {
                        cb.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                    //调用订单类服务
                    orderService.createOrder();
                }
            }).start();

        }

    }


}
